package com.hrt.base;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.hrt.bean.HRTMember;
import com.hrt.bean.UserVO;
import com.hrt.bean.ViewResource;
import com.hrt.manage.menus.service.ViewResourceService;
import com.hrt.util.PageUtils;
import com.hrt.util.WebParams;
import com.hrt.util.interceptor.RoleAccessInterceptor;

@Controller
public class BaseController<E>     {

	private Long totalCount = new Long(0);

	private Integer pageCount;

	private int pageSize = 10;

	private int pageIndex = 1;

	protected PageUtils<E> page;

	protected Class<E> clazz;

	protected List<E> list;

	private List<ViewResource> resList;

	@Autowired
	private ViewResourceService resService;;

	protected HttpServletRequest request;

	protected HttpServletResponse response;

	public List<E> getList() {
		return list;
	}

	public void setList(List<E> list) {
		this.list = new ArrayList();
		this.list = list;
	}

	public BaseController() {

	}

	/**
	 * 初始化函数
	 * 
	 * @param request
	 * @param response
	 */
	
//	private boolean init(HttpServletRequest request, HttpServletResponse response) {
//		
//
//
//	}

	public PageUtils<E> getPage() {

		if (getPageIndex() == 0) {
			setPageIndex(1);
		}
		page = new PageUtils<E>();
		page.setPageCount(getPageCount());
		page.setPageIndex(getPageIndex());
		page.setPageSize(getPageSize());
		page.setTotalCount(getTotalCount());
		page.setResultList((List<E>) getList());

		return page;
	}

	public void setPage(PageUtils<E> page) {
		this.page = page;
	}

	public Long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Long totalCount) {
		this.totalCount = totalCount;
	}

	public Integer getPageCount() {
		return pageCount;
	}

	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public Class<E> getClazz() {
		return clazz;
	}

	public void setClazz(Class<E> clazz) {
		this.clazz = clazz;
	}

	/**
	 *
	 * @param totalCount
	 * @param pageSize
	 * @return
	 */
	public Integer computePageCount(Long totalCount, int pageSize) {
		if (totalCount == null || totalCount == 0) {
			return 1;
		}
		if (totalCount % pageSize == 0) {
			return (int) (totalCount / pageSize);
		} else {
			return (int) (totalCount / pageSize) + 1;
		}

	}

	public Integer computePageCount() {
		if (totalCount == 0) {
			return 1;
		}
		if (getTotalCount() % pageSize == 0) {
			return (int) (getTotalCount() / pageSize);
		} else {
			return (int) (getTotalCount() / pageSize) + 1;
		}

	}

	public static Map<String, Object> transBean2Map(Object obj) {

		if (obj == null) {
			return null;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo
					.getPropertyDescriptors();
			for (PropertyDescriptor property : propertyDescriptors) {
				String key = property.getName();

				// 过滤class属性
				if (!key.equals("class")) {
					// 得到property对应的getter方法
					Method getter = property.getReadMethod();
					Object value = getter.invoke(obj);

					map.put(key, value);
				}

			}
		} catch (Exception e) {
			System.out.println("transBean2Map Error " + e);
		}

		return map;

	}

	protected UserVO getOnlineManager(HttpServletRequest request,
			HttpServletResponse response) {
		if (request.getSession().getAttribute(WebParams.ON_LINE_MANAGER) != null) {
			return (UserVO) request.getSession().getAttribute(
					WebParams.ON_LINE_MANAGER);
		}

		return null;
	}

	protected HRTMember getHRTMember(HttpServletRequest request,
			HttpServletResponse response) {
		if (request.getSession().getAttribute(WebParams.ON_LINE_MEMBER) != null) {

			return (HRTMember) request.getSession().getAttribute(
					WebParams.ON_LINE_MEMBER);
		}

		return null;
	}
	
	protected void setHRTMember(HRTMember member,HttpServletRequest request,
			HttpServletResponse response) {
		if (request.getSession().getAttribute(WebParams.ON_LINE_MEMBER) != null) {
			request.getSession().removeAttribute(WebParams.ON_LINE_MEMBER);
		}
		request.getSession().setAttribute(WebParams.ON_LINE_MEMBER, member);
	
	
	}

	protected void setOnlineManager(UserVO userVo, HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		if (request.getSession().getAttribute(WebParams.ON_LINE_MANAGER) != null) {
			request.getSession().removeAttribute(WebParams.ON_LINE_MANAGER);
		}
		request.getSession().setAttribute(WebParams.ON_LINE_MANAGER, userVo);
	}
	
	
//
//	/**
//	 * @param Integer
//	 *            isMember 0 检查后台Session 1 检查前台seesion
//	 */
//	@Override
//	public boolean isSessionTimeout(Integer isMember,
//			HttpServletRequest request, HttpServletResponse response) {
//		if (getOnlineManager(request, response) == null
//				|| this.getHRTMember(request, response) == null) {
//			
//			if (isMember.intValue() == 1) {
//				if (this.getHRTMember(request, response) == null) {
//
//					return true;
//				}
//			}
//			if (isMember.intValue() == 0) {
//				if (getOnlineManager(request, response) == null) {
//					return true;
//				}
//				
//				
//			}
//		}
//		return false;
//	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	

//	@Override
//	public void postHandle(HttpServletRequest request,
//			HttpServletResponse response, Object handler,
//			ModelAndView modelAndView) throws Exception {
//		 request.setCharacterEncoding("UTF-8");  
//	        response.setCharacterEncoding("UTF-8");  
//	        response.setContentType("text/html;charset=UTF-8");  
//	        String clzName = this.getClass().getName();
//			resList = new ArrayList<ViewResource>();
//			List<String> urlList = new ArrayList<String>();
//			String packageName = this.getClass().getPackage().getName();
//			System.out.println("class: " + clzName + "\n Package: " + packageName);
//
//			this.request = request;
//			this.response = response;
//			String requestUri = request.getRequestURI();
//			if (packageName.indexOf("com.hrt.manage") != -1) {
//
//				resList = resService.listManageMenuns(new Integer(0));
//			}
//			if (packageName.indexOf("com.hrt.web") != -1) {
//
//				resList = resService.listManageMenuns(new Integer(1));
//			}
//			if (resList != null && !resList.isEmpty()) {
//				for (ViewResource res : resList) {
//					urlList.add(res.getUrl());
//				}
//			}
//
//			for (String url : urlList) {
//				if (requestUri.indexOf(url) != -1) {
//					if (packageName.indexOf("com.hrt.manage") != -1) {
//						if (this.isSessionTimeout(new Integer(0), request, response)) {
//							try {
//								
//								System.out.println("------------->"+request.getContextPath());
//								response.sendRedirect(request.getContextPath()
//										+ "/manage/login.jsp");
//								
//
//							} catch (IOException e) {
//								// TODO Auto-generated catch block
//								e.printStackTrace();
//							}
//						}
//					}
//					if (requestUri.indexOf(url) != -1) {
//						if (packageName.indexOf("com.hrt.web") != -1) {
//
//							if (this.isSessionTimeout(new Integer(1), request,
//									response)) {
//								try {
//									System.out.println("------------->"+request.getContextPath());
//									response.sendRedirect(request.getContextPath()
//											+ "/HRTWEB/login.jsp");
//									
//
//								} catch (IOException e) {
//									// TODO Auto-generated catch block
//									e.printStackTrace();
//								}
//							}
//
//						}
//					}
//				}
//				
//			}
//			
//		 super.postHandle(request, response, handler,modelAndView);
//	}

	

}
